﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ERP_RORZE_SYSTEM;
using System.Globalization;
using System.Threading;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class panel_F5DailyTime : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        
        public panel_F5DailyTime()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadStaffList();
        }
       
        ///////////////////////////////////////////
        /// Load Sttaff
        ////////////////////////////////////////////
        private void LoadStaffList()
        {
            try
            {
                myConnection.Open();
                SqlCommand sql_cmd = myConnection.CreateCommand();
                sql_cmd.CommandText = "SELECT StaffID, StaffName FROM [222_Staff] WHERE StopDate IS NULL ORDER BY StaffID";
                SqlDataReader dataReader = sql_cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    string[] fullname = dataReader["StaffName"].ToString().Trim().Split(' ');

                    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
                    TextInfo textInfo = cultureInfo.TextInfo;
                    string txt = textInfo.ToTitleCase(fullname[fullname.Length - 1].ToLower());

                    item.Text = txt + dataReader["StaffID"].ToString().Trim();
                    item.Value = txt + dataReader["StaffID"].ToString().Trim();

                    this.staffID.Properties.Items.Add(item);                    
                }
                dataReader.Close();
                sql_cmd.Dispose();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }

        private void buttonView_Click(object sender, EventArgs e)
        {
            if (this.staffID.SelectedIndex < 0)
            {
                MessageBox.Show("Please select person make report");
            }
            else
            {
                ComboboxItem staff = (ComboboxItem)this.staffID.SelectedItem;

                reportDocument.DocumentSource = null;
                rpt_F5DailyTime F5DailyTime = new rpt_F5DailyTime();
                F5DailyTime.staffid = staff.Value.ToString();
                F5DailyTime.LoadData();
                F5DailyTime.CreateDocument();
                reportDocument.DocumentSource = F5DailyTime;
            }
        }
    }
}
